<html>

<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <meta content="initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <meta content="telephone=no" name="format-detection">
    <meta content="email=no" name="format-detection">
    <meta content="yes" name="apple-mobile-web-app-capable">
    <link href="https://res.wx.qq.com/open/libs/weui/1.1.3/weui.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f8f8f8;
            padding: 0;
            margin: 0;
        }

        .header {
            border-bottom: 1px solid #ccc;
            background-color: #fff;
        }

        .footer {
            margin: 0;
            padding: 0;
        }

        .footer p {
            text-align: center;
            color: #999;
            font-weight: 200;
            margin-top: 20px;
            font-size: 12px;
        }

        a {
            font-size: 14px !important;
            font-weight: 300;
            padding: 5px 20px;
        }

        .logo {
            text-align: center;
            padding-top: 60px;
        }

        .logo img {
            width: 70px;
            opacity: 0.8;
            border-radius: 50%;
        }

        .amount {
            font-size: 48px;
            text-align: center;
            color: #333;
        }

        .mchname {
            color: #999;
            margin: 0 20px;
            border-top: 1px solid #eee;
            padding: 12px;
            font-size: 13px;
            font-weight: 200;
        }

        .popfeng {
            position: absolute;
            bottom: 40px;
            left: 0;
            right: 0;
            background-color: #f8f8f8;
            line-height: 2px;
        }

        .popfeng p {
            color: #bfbfbf;
            font-size: 12px;
            text-align: center;
            width: 100%;
        }

        #loading {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(255, 255, 255, 0.8);
            z-index: 9999;
            text-align: center;
        }

        #loading .weui-toast {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: none;
        }
    </style>
    <title>微信收银台1</title>
</head>

<body class="hide">
<div class="header">
    <div class="logo">
        <img src="https://img.hbhlpay.com/uploads/images/20240724/20240724161439f83fd1186.png">
    </div>
    <div class="amount">¥ <span id="amount"></span></div>
    <div class="mchname">收款说明: <span id="description"></span></div>
</div>

<div class="footer" style="padding:40px 20px;">
    <a class="weui-btn weui-btn_primary" href="javascript:" id="payBtn">立即支付</a>
    <p>支付完成后, 如需退款请及时联系卖家</p>
    <div class="popfeng">
        <p>由 汇联科技 提供支付技术服务</p>
    </div>
</div>

<div id="toast" style="opacity: 0; display: none;">
    <div class="weui-mask_transparent"></div>
    <div class="weui-toast">
        <i class="weui-icon-success-no-circle weui-icon_toast"></i>
        <p class="weui-toast__content">已完成</p>
    </div>
</div>
<div id="loading" style="display: none;">
    <div class="weui-mask"></div>
    <div class="weui-toast">
        <i class="weui-loading weui-icon_toast"></i>
        <p class="weui-toast__content">加载中...</p>
    </div>
</div>
<script src="https://cdn.bootcss.com/zepto/1.2.0/zepto.min.js"></script>
<script>
    // 显示加载提示
    function showLoading() {
        document.getElementById('loading').style.display = 'block';
    }

    // 隐藏加载提示
    function hideLoading() {
        document.getElementById('loading').style.display = 'none';
    }

    // let domain = 'https://test-api.hbhlpay.com';
    let domain = window.location.origin;
    let localUrl = window.location.href;
    // 获取URL中的 payOrderNo 参数
    const urlParams = new URLSearchParams(window.location.search);
    const payOrderNo = urlParams.get('payOrderNo');
    const authCode = urlParams.get('code');
    if (!authCode || authCode == 'null') {
        getAuthPath();
    }

    function getAuthPath() {
        fetch(`${domain}/openapi/oauth/wechat/auth?redirectUrl=${localUrl}`, {
            method: "GET",
            headers: new Headers({
                "Content-Type": "application/json;charset=UTF-8",
            }),
        })
            .then(response => response.json())
            .then(data => {
                // // 假设后端返回支付信息并将其赋值给 str
                if (data.code === 200) {
                    window.location.href = data.url;
                } else {
                    alert('##bb ' + data.msg);
                }
            })
            .catch(error => {
                console.error('请求支付信息失败:', error);
            }).finally(() => {
            // 请求完成后，无论成功还是失败，都隐藏加载提示
            hideLoading();
        });
    }

    let payInfo;
    // 发送异步请求到后端，获取支付信息
    if (payOrderNo && authCode) {
        // 显示加载提示
        showLoading();
        fetch(`${domain}/openapi/pay/pre/create`, {
            method: "POST",
            headers: new Headers({
                "Content-Type": "application/json;charset=UTF-8",
            }),
            body: JSON.stringify({payOrderNo, authCode})
        })
            .then(response => response.json())
            .then(data => {
                // 假设后端返回支付信息并将其赋值给 str
                if (data.code === 200) {
                    payInfo = JSON.parse(data.data.payInfo);
                    document.getElementById("amount").textContent = data.data.amount;
                    document.getElementById("description").textContent = data.data.subject;
                    callpay();  // 自动发起支付请求
                } else {
                    alert('aaa  ' + data.msg)
                    console.error('请求支付信息失败:', data.msg);
                }
            })
            .catch(error => {
                console.error('请求支付信息失败:', error);
            }).finally(() => {
            // 请求完成后，无论成功还是失败，都隐藏加载提示
            hideLoading();
        });
    }

    //调用微信JS api 支付
    function jsApiCall() {
        WeixinJSBridge.invoke(
            "getBrandWCPayRequest",
            payInfo,
            function (res) {
                switch (res.err_msg) {
                    case "get_brand_wcpay_request:fail":
                    case "get_brand_wcpay_request:cancel":
                        const res = confirm("系统检测到交易失败拦截或取消支付，是否重新进行支付？")
                        if (res) {
                            fetch("/", {
                                method: "POST",
                                headers: new Headers({
                                    "Content-PictureType": "application/json;charset=UTF-8",
                                }),
                                body: JSON.stringify({payOrderNo: payOrderNo}),
                            })
                                .then((resolve) => resolve.json())
                                .then((res) => {
                                        if (res.code === 1) {
                                            payInfo = ''
                                            jsApiCall()
                                        }
                                    }
                                )
                        }
                        break;
                    case "get_brand_wcpay_request:ok":
                        window.location.href = "/";
                    default:
                        break;
                }
            }
        )
    }

    $('#payBtn').on('click', callpay);

    function callpay() {
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
            } else if (document.attachEvent) {
                document.attachEvent("WeixinJSBridgeReady", jsApiCall);
                document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
            }
        } else {
            jsApiCall();
        }
    }

    // callpay()
</script>

</body>

</html>